package com.ykdl.member.kid.gears;

import oauth.signpost.http.HttpParameters;
import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.ArticleListBean;
import com.ykdl.member.kid.beans.ResultBean;
import com.ykdl.member.kid.gears.ArticlesActivity.getDataTag;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class InputCodeActivity extends BaseScreen{
	private EditText input_code;
	private ImageView clear_bt ;
	private String code ;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("手动输码", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {

				finish();
			}
		}, R.string.turn_ok, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				senddata();
			}
		}, R.drawable.title_bg, false);
		setContent(R.layout.inputcodeactivity_xml);
		inint();
	}
	public void inint() {
		input_code = (EditText)findViewById(R.id.input_code);
		input_code.setInputType(EditorInfo.TYPE_CLASS_PHONE);
		clear_bt = (ImageView)findViewById(R.id.clear_bt);
		clear_bt.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				input_code.setText("");
			}
		});
	}
	public void senddata() {
		code = input_code.getText().toString().trim();
		if (code.length()>1&&code.startsWith("1")) {
			code=code.substring(1);
			Log.e("alan", "------code:"+code);
			showProgress("正在加载中,请稍等...");
			String  url =KidConfig.INPUT_CODE;
			HttpParameters parameterss = new HttpParameters();
//			parameterss.put("sig", "65b37a2a7dba87d27cbc4017ab3957dc");
			parameterss.put("code", code);
			HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(url, parameterss, null);
			 TaskManager.startHttpRequest(request, new senddataTag(),  ResultBean.class);
		}else if (code.length()==0) {
			Toast.makeText(mContext, "请输入二维码", 1).show();
		}else{
			Toast.makeText(mContext, "您输入的码不正确", 1).show();
		}
		
		
	}
	
	class senddataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof ResultBean) {
				ResultBean bean =(ResultBean)t;
				if (null==bean.getError()) {
					Toast.makeText(mContext, "积分成功", 1).show();
					InputCodeActivity.this.finish();
				}else {
					Toast.makeText(mContext, "失败"+bean.getDesc(), 1).show();
				}
				
			}
		
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	}
